package com.lj.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lj.common.core.model.system.SysRole;
import com.lj.system.mapper.SysRoleMapper;
import com.lj.system.service.ISysRoleService;
import org.apache.commons.compress.utils.Sets;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * 角色服务
 *
 * @author 小李飞刀
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Resource
    private SysRoleMapper roleMapper;

    /**
     * 根据用户ID查询权限集合
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public Set<String> selectRolePermissionByUserId(Long userId) {
        List<SysRole> perms = roleMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = Sets.newHashSet();
        for (SysRole perm : perms) {
            if (ObjectUtil.isNotNull(perm)) {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }

    /**
     * 根据用户ID查询已经选配置的角色列表
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public List<SysRole> selectRoleListByUserId(Long userId) {
        return roleMapper.selectRolePermissionByUserId(userId);
    }
}
